﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Bymed.SpineScan.DataModel;
using Bymed.SpineScan.WPF.Model;
using Bymed.SpineScan.WPF.Shared;
using Bymed.SpineScan.Service.Shared;
using System.Collections.Generic;

namespace Bymed.SpineScan.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class CustomizedTestItemViewModel : MyViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the CustomizedTestItemViewModel class.
        /// </summary>
        public CustomizedTestItemViewModel(IDataService dataService)
            : base(dataService) { }

        public override void ResetProperties()
        {
            try
            {
                base.ResetProperties();

                LoadCustomizedTestItemList();
            }
            catch (Exception ex)
            {
                RequestDisplayErrorMessage(ex.Message);
            }
        }

        /// <summary>
        /// The <see cref="SelectedCustomizedTestItem" /> property's name.
        /// </summary>
        public const string SelectedCustomizedTestItemPropertyName = "SelectedCustomizedTestItem";

        private object _selectedCustomizedTestItem = null;

        /// <summary>
        /// Sets and gets the SelectedCustomizedTestItem property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public object SelectedCustomizedTestItem
        {
            get
            {
                return _selectedCustomizedTestItem;
            }

            set
            {
                if (_selectedCustomizedTestItem == value)
                {
                    return;
                }

                RaisePropertyChanging(SelectedCustomizedTestItemPropertyName);
                _selectedCustomizedTestItem = value;
                RaisePropertyChanged(SelectedCustomizedTestItemPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="CustomizedTestItemList" /> property's name.
        /// </summary>
        public const string CustomizedTestItemListPropertyName = "CustomizedTestItemList";

        private ObservableCollection<CustomizedTestItem> _customizedTestItemList = new ObservableCollection<CustomizedTestItem>();

        /// <summary>
        /// Sets and gets the CustomizedTestItemList property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<CustomizedTestItem> CustomizedTestItemList
        {
            get
            {
                return _customizedTestItemList;
            }

            set
            {
                if (_customizedTestItemList == value)
                {
                    return;
                }

                RaisePropertyChanging(CustomizedTestItemListPropertyName);
                _customizedTestItemList = value;
                RaisePropertyChanged(CustomizedTestItemListPropertyName);
            }
        }

        private RelayCommand<int> _moveUpCommand;

        /// <summary>
        /// Gets the MoveUpCommand.
        /// </summary>
        public RelayCommand<int> MoveUpCommand
        {
            get
            {
                return _moveUpCommand
                    ?? (_moveUpCommand = new RelayCommand<int>(
                                          p =>
                                          {
                                              SelectedCustomizedTestItem = GeneralUtility.ChangeSelectedItemIndex<CustomizedTestItem>(CustomizedTestItemList,
                                                  p, p == 0 ? CustomizedTestItemList.Count - 1 : p - 1);
                                          },
                                          p => p == -1 ? false : true));
            }
        }

        private RelayCommand<int> _moveDownCommand;

        /// <summary>
        /// Gets the MoveDownCommand.
        /// </summary>
        public RelayCommand<int> MoveDownCommand
        {
            get
            {
                return _moveDownCommand
                    ?? (_moveDownCommand = new RelayCommand<int>(
                                          p =>
                                          {
                                              SelectedCustomizedTestItem = GeneralUtility.ChangeSelectedItemIndex<CustomizedTestItem>(CustomizedTestItemList,
                                                  p, p == CustomizedTestItemList.Count - 1 ? 0 : p + 1);
                                          },
                                          p => p == -1 ? false : true));
            }
        }

        private RelayCommand _saveCommand;

        /// <summary>
        /// Gets the SaveCommand.
        /// </summary>
        public RelayCommand SaveCommand
        {
            get
            {
                return _saveCommand
                    ?? (_saveCommand = new RelayCommand(
                                          () =>
                                          {
                                              try
                                              {
                                                  ServiceFactory.CreateCustomizedTestItemService().Synchronize(CustomizedTestItemList);

                                                  // Send ReturnHomeMessage to PageBase.
                                                  Messenger.Default.Send<int>(0, MainViewModel.ReturnHomeMessageToken);
                                              }
                                              catch (Exception ex)
                                              {
                                                  RequestDisplayErrorMessage(ex.Message);
                                              }
                                          }));
            }
        }

        /// <summary>
        /// Load the data from data source for initializing CustomizedTestItemList. 
        /// </summary>
        private void LoadCustomizedTestItemList()
        {
            var customizedTestItemList = ServiceFactory.CreateCustomizedTestItemService().GetAll().OrderBy(m => m.OrderIndex);
            CustomizedTestItemList = new ObservableCollection<CustomizedTestItem>(customizedTestItemList);
        }
    }

    #region For DataGridComboBoxColumn binding ONLY
    public class TestItemTypeListWrapper
    {
        public List<TestItemType> TestItemTypeList { get; private set; }

        public TestItemTypeListWrapper()
        {
            TestItemTypeList = new List<TestItemType>();

            TestItemTypeList.Add(new TestItemType
            {
                ItemName = ViewModelHelper.TestItemLateralBendingTypeTitle,
                ItemType = ViewModelHelper.TestItemLateralBendingTypeName
            });

            TestItemTypeList.Add(new TestItemType
            {
                ItemName = ViewModelHelper.TestItemFlexExtTypeTitle,
                ItemType = ViewModelHelper.TestItemFlexExtTypeName
            });

            TestItemTypeList.Add(new TestItemType
            {
                ItemName = ViewModelHelper.TestItemRotationTypeTitle,
                ItemType = ViewModelHelper.TestItemRotationTypeName
            });

            TestItemTypeList.Add(new TestItemType
            {
                ItemName = ViewModelHelper.TestItemBalanceTypeTitle,
                ItemType = ViewModelHelper.TestItemBalanceTypeName
            });
        }

        public List<TestItemType> GetTestItemTypeList()
        {
            return TestItemTypeList;
        }
    }

    public class TestItemType
    {
        public string ItemName { get; set; }
        public string ItemType { get; set; }
    }
    #endregion
}